// pages/order/index.js
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeIndex:0,
    tabBarArr:["全部","待付款","待收货","已完成","已取消"],
    orders:[
      {
        amount: "46.00",
        cost_detail: {freight_cost: 0, discount_coust: 0, goods_cost: "4600", coupons: [], coupons_money: 0},
        created_at: "2021-03-10 09:17:44",
        num: 1,
        order_goods: [
          {
            cover: "https://yceat-20200506.oss-cn-shanghai.aliyuncs.com/goods/cover/9bddd43bcb40b1464104f4f91b808b22.png",
            goods_sn: "20200902155347681720",
            group_time_limit: 0,
            id: "3596",
            is_cheap: 0,
            needidcard: 0,
            num: 1,
            pintuan: 0,
            price: 79,
            sell_price: 46,
            sku_id: 2965,
            start_group_nums: 0,
            supplier: "自营",
            tags: "初桃粉",
            title: "蕉下   冰薄系列防晒围巾【初桃粉】",
            vip_price: 46
          }
        ],
        order_no: "202103100917440001",
        related_no: "2103100917440001",
        state: -1,
        state_text: "已取消",
        type: 1,
        uid: 3877,
        updated_at: "2021-03-10 09:17:44",
      },
      {
        amount: "933.00",
        cost_detail: {freight_cost: 0, discount_coust: 0, goods_cost: "93300", coupons: [], coupons_money: 0},
        created_at: "2021-03-11 14:20:36",
        num: 4,
        order_goods: [
        {
        cover: "https://yceat-20200506.oss-cn-shanghai.aliyuncs.com/goods/cover/1ae1a03f92d926079a12d28a4a9b4a1f.jpg",
        goods_sn: "20200820144655430975",
        group_time_limit: 0,
        id: "3366",
        is_cheap: 0,
        needidcard: 0,
        num: 1,
        pintuan: 0,
        price: 259,
        sell_price: 229,
        sku_id: 2887,
        start_group_nums: 0,
        supplier: "自营",
        tags: null,
        title: "YUMC男士背包学生书包电脑旅行双肩背包休闲时尚B2085",
        vip_price: 229,
        },
         {
        cover: "https://yceat-20200506.oss-cn-shanghai.aliyuncs.com/goods/cover/393e929bd7207711e99e79dd7a682419.jpg",
        goods_sn: "20200902155342220202",
        group_time_limit: 0,
        id: "3585",
        is_cheap: 0,
        needidcard: 0,
        num: 1,
        pintuan: 0,
        price: 229,
        sell_price: 108,
        sku_id: 2950,
        start_group_nums: 0,
        supplier: "自营",
        tags: "魅紫色",
        title: "蕉下  天际系列防晒帽防紫外线防护帽  【魅紫色】",
        vip_price: 108
        },
      ],
        order_no: "202103111420360001",
        related_no: "2103111420360001",
        state: 0,
        state_text: "等待付款",
        type: 1,
        uid: 3877,
        updated_at: "2021-03-11 14:20:36",
      }
    ]
  },

  //点击取消订单按钮
  cancelOrder(e){
    console.log(e)
    const { id }=e.currentTarget.dataset;
    console.log(id)
    const orders=this.data.orders;
    const i=orders.findIndex(item=>item.order_no===id)
    Dialog.confirm({
      message: '确认要取消订单吗?',
    })
      .then(() => {
        // on confirm
        console.log(i)
        const temp=`orders[${i}].state`;
        const temp2=`orders[${i}].state_text`;
        //向后台发送请求并更新数据

        //修改本地数据
        this.setData({
          [temp]:-1,
          [temp2]:"已取消"
        },()=>{
          Dialog.alert({
            message: '订单已取消',
          }).then(() => {
            // on close
          });
        })
      })
      .catch(() => {
        // on cancel
      });
  },
  //点击已取消旁边的删除图标
  clearOrder(e){
    const { id }=e.currentTarget.dataset;
    console.log(id)
    const orders=this.data.orders;
    const i=orders.findIndex(item=>item.order_no===id)
    Dialog.confirm({
      message: '确认要清除订单信息吗?',
    })
      .then(() => {
        // on confirm
        //发送请求已出清单并更新本地数据

        const orders=this.data.orders;
        orders.splice(i,1);
        this.setData({
          orders
        },()=>{
          Dialog.alert({
            message: '订单已清除',
          }).then(() => {
            // on close
          });
        })
      })
      .catch(() => {
        // on cancel
      });
  },

  //点击tab栏切换
  onChange(e){
    // console.log(e)
    //根据e获取用户订单信息
  },

  //点击订单跳转订单页
  goToPageOrder(e){
    const { num }=e.currentTarget.dataset;
    const orders=this.data.orders;
    //在有接口的情况下根据订单编号跳转获取订单信息

    //以下是没有接口测试使用
    const i=orders.findIndex(item=>item.order_no===num);
    const orderInfo=JSON.stringify(orders[i]);
    wx.navigateTo({
      url: '/pages/orderInfo/orderInfo?detail='+orderInfo
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})